package it.getta.vicinometro.adapter;

import it.getta.vicinometro.R;
import it.getta.vicinometro.model.Place;
import it.getta.vicinometro.model.PlaceList;
import it.getta.vicinometro.utils.FileCache;
import it.getta.vicinometro.utils.ImageLoader;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PlacesPopupAdapter extends BaseAdapter {

	private OnInfoPressedListener mOnInfoPressedListener;
	private OnSetAlarmPressedListener mOnSetAlarmPressedListener;
	private LayoutInflater mInflater;
	public ImageLoader mImageLoader; 
	protected PlaceList mPlaces;
	
	public interface OnInfoPressedListener {
		public void infoPressed(Place place);
	}
	
	public interface OnSetAlarmPressedListener {
		public void setAlarmPressed(Place place);
	}
	
	public PlacesPopupAdapter(Context context, PlaceList places, FileCache fileCache) {
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mImageLoader=new ImageLoader(context, fileCache);
		mPlaces = places;
	}
	
	public void setOnInfoPressedListener(OnInfoPressedListener listener) {
		mOnInfoPressedListener = listener;
	}
	
	public void setOnSetAlarmPressedListener(OnSetAlarmPressedListener listener) {
		mOnSetAlarmPressedListener = listener;
	}
	
	@Override
	public int getCount() {
		return mPlaces.getResults().size();
	}

	@Override
	public Place getItem(int position) {
		return mPlaces.getResults().get(position);
	}
	

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			view = mInflater.inflate(R.layout.place_list_item, null);
		}
		
		ImageView image = (ImageView)view.findViewById(R.id.gallery_image);
		mImageLoader.DisplayImage(getItem(position).getIcon(), image);
		TextView title = (TextView)view.findViewById(R.id.place_list_item_title);
		TextView subTitle = (TextView)view.findViewById(R.id.place_list_item_subtitle);
		TextView subSubTitle = (TextView)view.findViewById(R.id.place_list_item_subsubtitle);
		
		title.setText(getItem(position).getName());
		StringBuilder sBuilder = new StringBuilder("");
		for (int i=0; i<getItem(position).getTypes().length; i++){
			if(i<getItem(position).getTypes().length - 1) {
				sBuilder.append(getItem(position).getTypes()[i] + ", ");
			} else {
				sBuilder.append(getItem(position).getTypes()[i]);
			}
			
		}
		subTitle.setText(sBuilder.toString());
		subSubTitle.setText(getItem(position).getVicinity());
		
		View toolbar = view.findViewById(R.id.place_list_item_toolbar);
		Button infoButton = (Button) toolbar.findViewById(R.id.place_list_toolbar_info);
		infoButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mOnInfoPressedListener != null)
					mOnInfoPressedListener.infoPressed(getItem(position));
			}
		});
		Button setButton = (Button) toolbar.findViewById(R.id.place_list_toolbar_set_alarm);
		setButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mOnSetAlarmPressedListener != null)
					mOnSetAlarmPressedListener.setAlarmPressed(getItem(position));
			}
		});
		
		// Resets the toolbar to be closed
        ((LinearLayout.LayoutParams) toolbar.getLayoutParams()).bottomMargin = -50;
        toolbar.setVisibility(View.GONE);
		
		return view;
	}


}
